package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 * A5论坛
 */
public class Admin5BbsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url){
		boolean bRet = false;
		String[] regex = {
				"http://www.admin5.net/thread-[0-9]*-1-1.html",
				"http://www.admin5.net/forum.php\\?mod=viewthread&tid=[0-9]*",
				"http://www.a5.net/forum.php?goto=lastpost&mod=redirect&tid=[0-9]*",
				"http://www.a5.net/forum.php\\?.*mod=viewthread[&page=1]*&tid=[0-9]*"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		String author = null;
		Integer clickNum=0;
		Integer commentNum=0;

		Document doc = Jsoup.parse(htmltxt);
		title = doc.select("title").text();
		title = StringUtils.substringBefore(title, " - ");
		if(StringUtils.isBlank(title)){
			title =doc.select("div.first div.bdtitle h1").text();
		}

		Element firstDiv = doc.select("div#postlist > div[id^=post_]").first();
		if (firstDiv!=null){
			Element firstA = firstDiv.select("div.authi>a[href]").first();
			if (firstA!=null){author =firstA .text();}
			date = DateUtils.matchDate(firstDiv.select("div.authi").text());
			content = HtmlCleaner.getContentHtml(url,  doc.select("td.t_f"));
		}
		if(date==null){
			String temp = doc.select("div.first div.bddot").text();
			date = DateUtils.matchDate(temp);
			content = HtmlCleaner.getContentHtml(url,  doc.select("div.first div.bdcontent"));
			author = StringUtils.substringBetween(temp, "作者： ", "发表于");
			String text = StringUtils.substringAfter(temp, "查看:");
			clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(text, "回复:"));
			commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(text, "回复:"));
		}
		String commTxt = doc.select("td.pls:contains(回复)").text();
		if(StringUtils.isNotBlank(commTxt)){
			clickNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "查看"));
			commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "回复"));
		}


		if(StringUtils.isBlank(title)){
		}
		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);
		bbs.setTitle(StringUtils.trimSpace(title));
		bbs.setContent(content);
		bbs.setAuthor(StringUtils.trim(author));
		bbs.setDate(date);
		bbs.setUpdateUrl(url);
		return bbs;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}
    
	
}
